package com.ruoyi.wb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wb.domain.WbOrganizationEnter;
import com.ruoyi.wb.dto.WbOrganizationEnterDto;
import com.ruoyi.wb.enmus.WbAuditType;
import com.ruoyi.wb.mapper.WbOrganizationEnterMapper;
import com.ruoyi.wb.service.IWbMaintainCompanyService;
import com.ruoyi.wb.service.IWbOrganizationEnterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 机构入驻
Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-02
 */
@Service
public class WbOrganizationEnterServiceImpl implements IWbOrganizationEnterService 
{
    @Autowired
    private WbOrganizationEnterMapper wbOrganizationEnterMapper;

    @Autowired
    private IWbMaintainCompanyService iWbMaintainCompanyService;

    /**
     * 查询机构入驻
     * @param id 机构入驻主键
     * @return 机构入驻

     */
    @Override
    public WbOrganizationEnter selectWbOrganizationEnterById(Long id)
    {
        return wbOrganizationEnterMapper.selectWbOrganizationEnterById(id);
    }

    /**
     * 查询机构入驻列表
     * @param wbOrganizationEnter 机构入驻
     * @return 机构入驻

     */
    @Override
    public List<WbOrganizationEnter> selectWbOrganizationEnterList(WbOrganizationEnter wbOrganizationEnter)
    {
        return wbOrganizationEnterMapper.selectWbOrganizationEnterList(wbOrganizationEnter);
    }

    /**
     * 新增机构入驻
     * 
     * @param wbOrganizationEnter 机构入驻
     * @return 结果
     */
    @Override
    public int insertWbOrganizationEnter(WbOrganizationEnter wbOrganizationEnter)
    {

        //查询是否已经新增过
        SysUser user = SecurityUtils.getLoginUser().getUser();
        WbOrganizationEnter item = new WbOrganizationEnter();
        item.setCreateBy(user.getUserId().toString());
        List<WbOrganizationEnter> wbOrganizationEnters = wbOrganizationEnterMapper.selectWbOrganizationEnterList(item);
        long count = wbOrganizationEnters.stream().filter(t -> !t.getAuditType().equals(3L)).count();
        if (count>0){
            throw new RuntimeException("您已经申请过入驻无法再次申请");
        }


        wbOrganizationEnter.setResponsibleName(user.getUserName());
        wbOrganizationEnter.setResponsiblePhone(user.getPhonenumber());
        wbOrganizationEnter.setAuditType(1L);
        wbOrganizationEnter.setCreateBy(user.getUserId().toString());
        wbOrganizationEnter.setCreateTime(DateUtils.getNowDate());
        return wbOrganizationEnterMapper.insertWbOrganizationEnter(wbOrganizationEnter);
    }

    /**
     * 修改机构入驻
     */
    @Override
    @Transactional
    public int updateWbOrganizationEnter(WbOrganizationEnterDto wbOrganizationEnterDto)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        WbOrganizationEnter wbOrganizationEnter = BeanUtil.copyProperties(wbOrganizationEnterDto, WbOrganizationEnter.class);
//        if (ObjectUtils.isEmpty(wbOrganizationEnterDto.getAuditType())){
//            throw new RuntimeException("审核状态必填");
//        }
        if (ObjectUtils.isEmpty(wbOrganizationEnterDto.getId())){
            throw new RuntimeException("编号必传");
        }
        if (!ObjectUtils.isEmpty(wbOrganizationEnterDto.getAuditType())&&wbOrganizationEnterDto.getAuditType().equals(WbAuditType.BEENTERING.getCode())){
            wbOrganizationEnterDto.setBusinessLicense(wbOrganizationEnterDto.getWbMaintainCompanyDto().getBusinessLicense());
//            WbOrganizationEnter item = wbOrganizationEnterMapper.selectWbOrganizationEnterById(wbOrganizationEnterDto.getId());
//            wbOrganizationEnterDto.getWbMaintainCompanyDto().setUserId(Long.parseLong(item.getCreateBy()));
            Long maintainCompanyId = iWbMaintainCompanyService.insertWbMaintainCompany(wbOrganizationEnterDto.getWbMaintainCompanyDto());
            wbOrganizationEnter.setMaintainCompanyId(maintainCompanyId);//填充维保公司id
        }

        WbOrganizationEnter wbOrganizationEnter1 = wbOrganizationEnterMapper.selectWbOrganizationEnterById(wbOrganizationEnter.getId());

        if(!ObjectUtils.isEmpty(wbOrganizationEnter.getAuditType())&&
                wbOrganizationEnter.getAuditType().equals(WbAuditType.BEENTERING.getCode())&&
                !wbOrganizationEnter1.getAuditType().equals(WbAuditType.BEENTERING.getCode())
        ){
            wbOrganizationEnter.setCauseOfRejection("您的机构入驻申请已经通过,请保持手机畅通。");
            wbOrganizationEnter.setIfRead(1L);

        }
        if((!ObjectUtils.isEmpty(wbOrganizationEnter.getAuditType())&&
                wbOrganizationEnter.getAuditType().equals(WbAuditType.BEENTERING.getCode())&&
                !wbOrganizationEnter1.getAuditType().equals(WbAuditType.BEENTERING.getCode()))||
                (!ObjectUtils.isEmpty(wbOrganizationEnter.getAuditType())&&
                        wbOrganizationEnter.getAuditType().equals(WbAuditType.NOTSETTLED.getCode()))
        ){
            wbOrganizationEnter.setUpdateTime(DateUtils.getNowDate());
            wbOrganizationEnter.setUpdateBy(user.getNickName());
        }

        return wbOrganizationEnterMapper.updateWbOrganizationEnter(wbOrganizationEnter);
    }

    /**
     * 批量删除机构入驻

     * 
     * @param ids 需要删除的机构入驻
主键
     * @return 结果
     */
    @Override
    public int deleteWbOrganizationEnterByIds(Long[] ids)
    {
        return wbOrganizationEnterMapper.deleteWbOrganizationEnterByIds(ids);
    }

    /**
     * 删除机构入驻
信息
     * 
     * @param id 机构入驻
主键
     * @return 结果
     */
    @Override
    public int deleteWbOrganizationEnterById(Long id)
    {
        return wbOrganizationEnterMapper.deleteWbOrganizationEnterById(id);
    }

    @Override
    public WbOrganizationEnter byUser() {
        Long userId = SecurityUtils.getLoginUser().getUserId();

        return wbOrganizationEnterMapper.byUser(userId);
    }
}
